package d5_communication;

import java.util.ArrayList;
import java.util.List;

public class Desk {
    private List<String> list = new ArrayList<String>();
    public synchronized void put() {
        try {
            String name = Thread.currentThread().getName();
            if (list.size() == 0) {
                list.add(name+"做的包子");
                System.out.println(name+"做了个包子");
                Thread.sleep(2000);
                this.notifyAll();
                this.wait();
            }else{
                Thread.sleep(2000);
                System.out.println(name+"发现有包子");
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public synchronized void get() {
        try {
        String name = Thread.currentThread().getName();
        if (list.size() != 0) {
            System.out.println(name+"吃了"+list.get(0));
            list.remove(0);
            Thread.sleep(2000);
            this.notifyAll();
            this.wait();
        }else{
            Thread.sleep(2000);
            System.out.println(name+"发现没有包子");
            this.notifyAll();
            this.wait();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    }
}
